/**
 * test
   var bmp:Bitmap = new Bitmap(new BitmapData(2, 2, false, 0xff0000ff));
   var bmp2:Bitmap = new Bitmap(new BitmapData(2, 2, false, 0xff00ff00));
   addChilds(bmp);
   addChilds(bmp2);
 */
package mylist
{
	import com.greensock.TweenLite;
	import com.ViewSet;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	 * list Document Class
	 * ...
	 * @author db0@qq.com
	 */
	public class ViewList extends Sprite
	{
		public static var origX:int = 50;
		public static var origY:int = 50;
		public static var main:ViewList;
		
		private static var numCol:int = 5;
		private static var ShowRow:int = 5;
		private static var numRow:int = 0;
		
		
		public function ViewList()
		{
			if (main)
				return;
			main = this;
			//logs.adds("ViewList.main");
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			fullScrn();
			
			addEventListeners();
		}
		
		private function fullScrn():void
		{
			numCol = Math.floor(stage.stageWidth / IconItem.ww);
			ShowRow = Math.floor(stage.stageHeight / IconItem.hh);
			if (DragItem.willSend) {
				//ShowRow-- ;
			}else {
				numCol-- ;
				ShowRow-- ;
			}
			logs.adds(numCol, ShowRow,IconItem.ww,IconItem.hh,stage.stageWidth);
			origX = stage.stageWidth / 2 - numCol * IconItem.ww / 2;
			origY = stage.stageHeight / 2 - ShowRow * IconItem.hh / 2;
			mask = stage.addChild(new Bitmap(new BitmapData(IconItem.ww * ViewList.numCol, IconItem.hh * ViewList.ShowRow)));
			
			x = mask.x = origX;
			y = mask.y = origY;
			
			//stage.addChild(listMain.bigborder);
			//listMain.bigborder.x = origX - 1;
			//listMain.bigborder.y = origY - 1;
			//listMain.bigborder.width = mask.width + 2;
			//listMain.bigborder.height = mask.height + 2;
			//stage.addChild(new ScrollBar);
			stage.addChild(new DragItem);
			stage.addChild(new ShowBig);
		}
		
		public function clears():void
		{
			while (numChildren > 0)
			{
				removeItem(numChildren - 1);
			}
			//ScrollBar.main.visible = false;
		}
		
		public function removeItem(i:int):void
		{
			var obj:IconItem = getChildAt(i) as IconItem;
			obj.showthis();
			removeChild(obj);
			obj = null;
			FileItem.list.splice(i, 1);
			
			refresh();
		}
		
		public function refresh():void
		{
			var i:int = 0;
			while (i < numChildren)
			{
				var uploadItem:IconItem = getChildAt(i) as IconItem;
				uploadItem.setIndex(i);
				uploadItem.x = (i % numCol) * IconItem.ww;
				uploadItem.y = int(i / numCol) * IconItem.hh;
				++i;
			}
			//outof(ScrollBar.main.setShow);
		}
		
		public function addChilds(bmp:Bitmap):void
		{
			var index:int = numChildren;
			logs.adds(index,bmp.width,bmp.height);
		//stage.addChild(bmp);
		//return;
			if (FileItem.list.length > index)
			{
				FileItem.list[index].bmp = bmp;
			}
			else
			{
				FileItem.pushBmp(bmp);
			}
			
			var uploadItem:IconItem = new IconItem(bmp);
			uploadItem.setIndex(index);
			
			FileItem.list[index].uploadItem = uploadItem;
			
			uploadItem.x = (index % numCol) * (IconItem.ww);
			uploadItem.y = int(index / numCol) * (IconItem.hh);
			//logs.adds(uploadItem.x, uploadItem.y);
			addChild(uploadItem);
			
			numRow = Math.ceil(FileItem.list.length / numCol);
			
			if (main.height > main.mask.height)
			{
				//ScrollBar.main.setShow();
			}
			
			//logs.adds("addChilds(bmp:Bitmap);stat", FileItem.list[index].state);
			
			if (DragItem.willSend)
			{
				if(AndroidCamera.main)
				if (AndroidCamera.main.Bmp) {
					FileItem.list[index].jpeg = AndroidCamera.main.Bmp;
					AndroidCamera.main.setNull();
				}
				uploadItem.startSend();
			}
			else
			{
				if (index < numCol * ShowRow) {
					x = stage.stageWidth / 2 - width / 2;
					y = stage.stageHeight / 2 - height / 2;
				}
				FileItem.list[index].state = FileItem.UPLOADED;
			}
		}
		
		
		
		private function addEventListeners():void
		{
			addEventListener(MouseEvent.MOUSE_WHEEL, rolls);
			addEventListener(MouseEvent.MOUSE_DOWN, startDrags);
			//addEventListener(MouseEvent.MOUSE_MOVE, moves);
			//addEventListener(MouseEvent.MOUSE_OUT, moves);
		}
		
		private function startDrags(e:MouseEvent):void
		{
			if (!DragItem.willSend && numChildren < numCol * ShowRow) return;
			startDrag(false, new Rectangle(origX, origY - Math.max(height,mask.height), 0, Math.max(height,mask.height) + (mask.height)));
			stage.addEventListener(MouseEvent.MOUSE_UP, stopDrags);
			//listMain.showSmall.setShow();
			//ScrollBar.main.setShow();
		}
		
		//private function moves(e:MouseEvent):void
		//{
			//switch (e.type)
			//{
				//case MouseEvent.MOUSE_OUT: 
					//listMain.showSmall.visible = false;
					//break;
				//default: 
					//listMain.showSmall.setShow();
					//ScrollBar.main.setShow();
			//}
		//}
		
		private function stopDrags(e:MouseEvent):void
		{
			//stage.removeEventListener(MouseEvent.MOUSE_MOVE, moves);
			stage.removeEventListener(MouseEvent.MOUSE_UP, stopDrags);
			stopDrag();
			outof(null);
		}
		
		private function outof(onCompleteFunc:Function):void
		{
			TweenLite.killTweensOf(main);
			if (y >= origY)
			{
				TweenLite.to(main, .3, {y: origY, onComplete: onCompleteFunc});
			}
			else if (y + main.height < origY + mask.height)
			{
				if (height < mask.height)
					TweenLite.to(main, .3, {y: origY, onComplete: onCompleteFunc});
				else
					TweenLite.to(main, .3, {y: origY - height + main.mask.height, onComplete: onCompleteFunc});
			}
		}
		
		private function rolls(e:MouseEvent = null):void
		{
			if (!DragItem.willSend && numChildren < numCol * ShowRow) return;
			var curRow:int = int((origY - y) / IconItem.hh);
			if (e.type == MouseEvent.MOUSE_WHEEL)
			{
				if (e.delta > 0)
				{
					curRow--;
				}
				else if (e.delta < 0)
				{
					curRow++;
				}
			}
			if (curRow >= numRow - ShowRow)
				curRow = numRow - ShowRow;
			if (curRow <= 0)
				curRow = 0;
			if (curRow * numCol < FileItem.list.length)
				if (FileItem.list[curRow * numCol])
					y = origY - FileItem.list[curRow * numCol].uploadItem.y;
			
			//listMain.showSmall.setShow();
			//ScrollBar.main.setShow();
		
		}
	
	}

}